﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
    public class SellingViewModel : BaseViewModel
    {

        private ObservableCollection<SearchResultItem> _selling;
        public ObservableCollection<SearchResultItem> Selling
        {
            get { return _selling ?? (_selling = new ObservableCollection<SearchResultItem>()); }
        }

        private ObservableCollection<SearchResultItem> _sold;
        public ObservableCollection<SearchResultItem> Sold
        {
            get { return _sold ?? (_sold = new ObservableCollection<SearchResultItem>()); }
        }

        private ObservableCollection<SearchResultItem> _unsold;
        public ObservableCollection<SearchResultItem> Unsold
        {
            get { return _unsold ?? (_unsold = new ObservableCollection<SearchResultItem>()); }
        }

        private int _loadingCount;
        public bool IsLoading
        {
            get { return _loadingCount != 0; }
        }
        private void IncreaseLoadingCount()
        {
            _loadingCount++;
            NotifyPropertyChanged("IsLoading");
        }
        private void DecreaseLoadingCount()
        {
            _loadingCount--;
            NotifyPropertyChanged("IsLoading");
        }

        public void LoadSelling()
        {
            IncreaseLoadingCount();
            MyTradeMeCriteria criteria = new MyTradeMeCriteria();
            App.TradeMe.MyTradeMe.MyTradeMeSellingItems(criteria, result =>
            {
                DecreaseLoadingCount();
                if (result.IsError)
                {
                    MessageBox.Show("Bugger: Unable to load sell list");
                    return;
                }

                Selling.Clear();

                foreach (var listing in result.Response.List)
                    Selling.Add(SearchResultItem.Build(listing));
            });
        }

        public void LoadSold()
        {
            IncreaseLoadingCount();
            MyTradeMeCriteria criteria = new MyTradeMeCriteria();
            App.TradeMe.MyTradeMe.MyTradeMeSoldItems(criteria, result =>
            {
                DecreaseLoadingCount();
                if (result.IsError)
                {
                    MessageBox.Show("Bugger: Unable to load sold list");
                    return;
                }

                Sold.Clear();
                foreach (var listing in result.Response.List)
                    Sold.Add(SearchResultItem.Build(listing));
            });
        }

        public void LoadUnsold()
        {
            IncreaseLoadingCount();
            MyTradeMeCriteria criteria = new MyTradeMeCriteria();
            App.TradeMe.MyTradeMe.MyTradeMeUnsoldItems(criteria, result =>
            {
                DecreaseLoadingCount();
                if (result.IsError)
                {
                    MessageBox.Show("Bugger: Unable to load unsold list");
                    return;
                }

                Unsold.Clear();
                foreach (var listing in result.Response.List)
                    Unsold.Add(SearchResultItem.Build(listing));
            });
        }
    }
}
